/*
 * Copyright (c) 2013 by Chances.
 * $CVSHeader$
 * $Author$
 * $Date$
 * $Revision$
 */
package chances.epg.search.analyzer;

import java.io.Reader;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.util.CharArraySet;

import chances.epg.search.LucenceVersion;


/**
 * Class comments.
 * 
 * @author <a href="mailto:libf@chances.com.cn">libf</a>
 */
public class SplitAnalyzer extends Analyzer {
    private CharArraySet stopWords;

    @Override
    protected TokenStreamComponents createComponents(final String fieldName,
            final Reader reader) {  
        return new TokenStreamComponents(new SplitTokenizer(LucenceVersion.VERSION,
                reader,getStopWords()));
    }
    
    //自定义分词字符
    private CharArraySet getStopWords() {
        if(stopWords == null){
            stopWords = new CharArraySet(LucenceVersion.VERSION,4,false);
            stopWords.add(",");//半角逗号
            stopWords.add("，");//全角逗号
            stopWords.add("、");//全角顿号
            stopWords.add("/");//反斜杠
            stopWords.add("|");//竖杠
        }
        
        return stopWords;
    }

    public static void main(String[] args) {
        Analyzer newStop = new SplitAnalyzer();
        
        try {
            TokenStream stream = newStop.tokenStream("test", new StringReader("罗宾·威廉 斯瑞克·吉瓦斯|"));
            CharTermAttribute termAtt = stream
                    .getAttribute(CharTermAttribute.class);
            stream.reset();
            int i = 0;
            while (stream.incrementToken()) {
                String text = termAtt.toString();
                System.out.println("i" + text);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
			newStop.close();
		}
    }
}
